package com.sansan.qiangji.realize.board;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.BaseWageAccount.WageAccountVO;
import com.sansan.qiangji.VO.board.*;
import com.sansan.qiangji.entity.base.Device;
import com.sansan.qiangji.entity.base.Shelve;
import com.sansan.qiangji.entity.base.Warehouse;
import com.sansan.qiangji.entity.base.example.ShelveExample;
import com.sansan.qiangji.entity.base.example.WarehouseExample;
import com.sansan.qiangji.form.base.BaseWageAccountListForm;
import com.sansan.qiangji.form.base.ShelveListForm;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.service.inter.base.BaseWageAccountService;
import com.sansan.qiangji.service.inter.base.ShelveService;
import com.sansan.qiangji.service.inter.base.StockService;
import com.sansan.qiangji.service.inter.base.WarehouseService;
import com.sansan.qiangji.service.inter.warehouse.PullOrderBoxDetailService;
import com.sansan.qiangji.service.inter.warehouse.PushOrderBoxDetailService;
import com.sansan.qiangji.utils.DateUtil;
import com.sansan.qiangji.utils.ResultVOUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BoardReal {

    @Autowired
    private PushOrderBoxDetailService pushOrderBoxDetailService;
    @Autowired
    private PullOrderBoxDetailService pullOrderBoxDetailService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private ShelveService shelveService;
    @Autowired
    private BaseWageAccountService baseWageAccountService;


    public ResultVO countBox7Days(){
        List<CountPushBoxVO> countPushBoxVOS = pushOrderBoxDetailService.countPushBox();
        List<CountPullBoxVO> countPullBoxVOS = pullOrderBoxDetailService.countPullBox();
        Date lastDate = DateUtil.dateAddNum(new Date(), -6);
        Date nowDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String lastDateString = format.format(lastDate);
        String nowDateString = format.format(nowDate);
        List<String> dateList = DateUtil.dayBetweenTwoDate(lastDateString, nowDateString);
        WarehouseExample warehouseExample = new WarehouseExample();
        List<Warehouse> warehouseList = warehouseService.selectByExample(warehouseExample);

        List<CountWarehouseVO> countWarehouseVOList = new ArrayList<>();
        for(Warehouse warehouse : warehouseList){
            CountWarehouseVO countWarehouseVO = new CountWarehouseVO();
            //库位利用率
            ShelveListForm shelveListForm = new ShelveListForm();
            shelveListForm.setWarehouseName(warehouse.getWarehouseName());
            List<ShelveUseRateVO> rateVOList = shelveService.shelveStockUseRate(shelveListForm);
            countWarehouseVO.setRateVOList(rateVOList);

            ShelveExample shelveExample = new ShelveExample();
            shelveExample.or().andWarehouseNameEqualTo(warehouse.getWarehouseName());
            List<Shelve> shelveList = shelveService.selectByExample(shelveExample);
            countWarehouseVO.setWareHouseName(warehouse.getWarehouseName());
            List<CountDateVO> countDateVOList = new ArrayList<>();
            for(String date : dateList){
                CountDateVO countDateVO = new CountDateVO();
                countDateVO.setDatestr(date);
                List<CountPullBoxVO> datePull = countPullBoxVOS.stream().filter(countPullBoxVO -> countPullBoxVO.getPullTime().equals(date)).collect(Collectors.toList());
                int datePullCount = datePull.stream().mapToInt(CountPullBoxVO::getBoxCount).sum();
                countDateVO.setDatePullCount(datePullCount);
                List<CountPushBoxVO> datePush = countPushBoxVOS.stream().filter(countPushBoxVO -> countPushBoxVO.getPushTime().equals(date)).collect(Collectors.toList());
                int datePushCount = datePush.stream().mapToInt(CountPushBoxVO::getBoxCount).sum();
                countDateVO.setDatePushCount(datePushCount);
                List<CountShelvesVO> countShelvesVOList = new ArrayList<>();
                for(Shelve shelve :shelveList){
                    CountShelvesVO countShelvesVO = new CountShelvesVO();
                    countShelvesVO.setShelveName(shelve.getShelveName());
                    List<CountPullBoxVO> shelvePull = datePull.stream().filter(countPullBoxVO -> countPullBoxVO.getSheveName().equals(shelve.getShelveName())).collect(Collectors.toList());
                    int shelvePullCount = shelvePull.stream().mapToInt(CountPullBoxVO::getBoxCount).sum();
                    List<CountPushBoxVO> shelvePush = datePush.stream().filter(countPushBoxVO -> countPushBoxVO.getSheveName().equals(shelve.getShelveName())).collect(Collectors.toList());
                    int shelvePushCount = shelvePush.stream().mapToInt(CountPushBoxVO::getBoxCount).sum();
                    countShelvesVO.setPullCountBox(shelvePullCount);
                    countShelvesVO.setPushCountbox(shelvePushCount);
                    countShelvesVOList.add(countShelvesVO);
                }
                countDateVO.setCountShelvesVOList(countShelvesVOList);
                countDateVOList.add(countDateVO);
                countWarehouseVO.setCountDateVOList(countDateVOList);
            }

            countWarehouseVOList.add(countWarehouseVO);
        }
        return ResultVOUtil.success(countWarehouseVOList);

    }

    //货位利用率
    public ResultVO shelveStockUseRate(ShelveListForm shelveListForm){
        List<ShelveUseRateVO> shelveUseRateVOS = shelveService.shelveStockUseRate(shelveListForm);
        int pageNum = shelveListForm.getPageNum();
        int pageSize = shelveListForm.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        PageResult<ShelveUseRateVO> ratePageResult = PageUtil.getPageResult(new PageInfo<>(shelveUseRateVOS));
        return ResultVOUtil.success(ratePageResult);

    }

    //工资核算
    public ResultVO getWageAccountVOList(BaseWageAccountListForm baseWageAccountListForm){
        List<WageAccountVO> accountVOList = baseWageAccountService.getWageAccountVOList(baseWageAccountListForm);
        int pageNum = baseWageAccountListForm.getPageNum();
        int pageSize = baseWageAccountListForm.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        PageResult<WageAccountVO> accountVOPageResult = PageUtil.getPageResult(new PageInfo<>(accountVOList));
        return ResultVOUtil.success(accountVOPageResult);
    }

}
